/*
 * Created by JFormDesigner on Wed Jun 16 23:11:57 GMT+08:00 2021
 */

package com.bk.view;

import java.awt.event.*;
import com.bk.model.User;
import com.bk.util.DbUtil;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Ayi
 */
// 登录窗口类
public class LoginFrame extends JFrame {
    public LoginFrame() {
        initComponents();
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    // 登录按钮
    private void loginButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
        ViewAction va = new ViewAction();
        User tUser = va.loginAction(userName.getText(), userPassword.getText());
        if (tUser != null) {
            JOptionPane.showMessageDialog(null, "登录成功！");
            dispose();
            if (tUser.getIsAdmin().equals("是")) {
                MainFrame mf = new MainFrame();
                mf.setTitle("欢迎您，" + tUser.getUname() + "，这里是管理端");
                mf.setVisible(true);
            }else{
                UserFrame uf = new UserFrame(tUser);
                uf.setTitle("欢迎您，" + tUser.getUname() + "，这里是借阅端（当前信用：" + tUser.getUcredit() + "）");
                uf.setVisible(true);
            }
        }else {
            JOptionPane.showMessageDialog(null, "用户名或密码有误/为空");
        }
    }

    // 重置，监听
    private void ClearButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
        userName.setText("");
        userPassword.setText("");
    }

    // 注册，监听
    private void RegButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
        dispose();
        new RegFrame().setVisible(true);
    }

    // 锚点重置 用户名 -> 密码
    private void userNameKeyPressed(KeyEvent e) {
        // TODO add your code here
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            userPassword.grabFocus();
        }
    }

    // 锚点重置 密码 -> 登录按钮
    private void userPasswordKeyPressed(KeyEvent e) {
        // TODO add your code here
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            loginButton.doClick();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        userName = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        regButton = new JButton();
        clearButton = new JButton();
        loginButton = new JButton();
        userPassword = new JPasswordField();

        //======== this ========
        setFont(new Font(Font.DIALOG, Font.PLAIN, 10));
        setTitle("\u767b\u5f55");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("  \u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf");
        label1.setIcon(new ImageIcon(getClass().getResource("/images/book1.gif")));
        label1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 18));

        //---- userName ----
        userName.setFont(userName.getFont().deriveFont(userName.getFont().getSize() + 5f));
        userName.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                userNameKeyPressed(e);
            }
        });

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        label2.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 16));

        //---- label3 ----
        label3.setText("\u5bc6\u7801 ");
        label3.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 16));

        //---- regButton ----
        regButton.setText("\u6ce8\u518c");
        regButton.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        regButton.setBackground(Color.white);
        regButton.addActionListener(e -> RegButtonActionPerformed(e));

        //---- clearButton ----
        clearButton.setText("\u91cd\u7f6e");
        clearButton.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        clearButton.setBackground(Color.white);
        clearButton.addActionListener(e -> ClearButtonActionPerformed(e));

        //---- loginButton ----
        loginButton.setText("\u767b\u5f55");
        loginButton.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        loginButton.setBackground(Color.white);
        loginButton.addActionListener(e -> loginButtonActionPerformed(e));

        //---- userPassword ----
        userPassword.setFont(userPassword.getFont().deriveFont(userPassword.getFont().getSize() + 5f));
        userPassword.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                userPasswordKeyPressed(e);
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(79, 79, 79)
                            .addComponent(loginButton, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
                            .addGap(31, 31, 31)
                            .addComponent(regButton, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
                            .addGap(30, 30, 30)
                            .addComponent(clearButton, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(91, 91, 91)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label3)
                                .addComponent(label2))
                            .addGap(31, 31, 31)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(userPassword, GroupLayout.PREFERRED_SIZE, 202, GroupLayout.PREFERRED_SIZE)
                                .addComponent(userName, GroupLayout.PREFERRED_SIZE, 202, GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap(67, Short.MAX_VALUE))
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(0, 137, Short.MAX_VALUE)
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 245, GroupLayout.PREFERRED_SIZE)
                    .addGap(86, 86, 86))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(24, 24, 24)
                    .addComponent(label1)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(33, 33, 33)
                            .addComponent(label2)
                            .addGap(37, 37, 37))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(userName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(32, 32, 32)))
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(userPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label3))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(loginButton)
                        .addComponent(regButton)
                        .addComponent(clearButton))
                    .addGap(43, 43, 43))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JTextField userName;
    private JLabel label2;
    private JLabel label3;
    private JButton regButton;
    private JButton clearButton;
    private JButton loginButton;
    private JPasswordField userPassword;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
